// 列表初始化
import { ref } from 'vue'
import { getSystemAccountListApi } from '@/api/system/systemAccount'
import { getUserVisitApi } from '@/api/common'
import { deepClone } from '@/util'
import globalData from '@/staticData/globalData'

const resetForm = () => {
  return {
    pageIndex: 1,
    pageSize: 20
  }
}
const searchForm = ref(resetForm())
const formItems = ref([
  {
    field: 'account',
    type: 'auto-complete',
    props: { label: '账号' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      maxlength: 32
    }
  },
  {
    field: 'jobNumber',
    type: 'auto-complete',
    props: { label: '工号' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      maxlength: 16
    }
  },
  {
    field: 'name',
    type: 'auto-complete',
    props: { label: '姓名' },
    attr: {
      placeholder: '请输入内容',
      querySearch: true,
      width: 400,
      maxlength: 32
    }
  },
  {
    field: 'businessLineIdList',
    type: 'select',
    props: { label: '数据授权' },
    attr: {
      placeholder: '请选择内容',
      multiple: true
    },
    options: []
  },
  {
    field: 'statusList',
    type: 'select',
    props: { label: '账号状态' },
    attr: {
      placeholder: '请选择内容',
      multiple: true
    },
    options: globalData.ACCOUNT_STATUS_LIST
  }
])
const columns = ref([
  {
    label: '工号',
    prop: 'jobNumber'
  },
  {
    label: '姓名',
    prop: 'name'
  },
  {
    label: '账号',
    prop: 'account'
  },
  {
    label: '数据授权',
    prop: 'businessLines'
  },
  {
    label: '更新时间',
    prop: 'modifiedDatetime',
    sortable: 'custom'
  },
  {
    label: '操作',
    prop: 'gender',
    slots: { customRender: 'action' }
  }
])
const businessLineList = ref([]) // 业务线数据
const total = ref(0)
const schemaTableRef = ref(null)

export default function useList() {
  // 获取业务线数据
  const getBusinessLineList = async() => {
    const res = await getUserVisitApi()
    console.log('业务线', res)
    businessLineList.value = res.map(item => {
      item.label = item.name
      item.value = item.id
      return item
    })
    formItems.value[3].options = businessLineList.value
  }
  // 查询数据
  const loadData = async val => {
    if (Object.keys(searchForm.value).length === 0) {
      searchForm.value = resetForm()
    }
    let params = {}
    params = deepClone(searchForm.value)
    console.log('查询参数', params)
    // 处理修改时间
    if (params.modifiedDatetime?.length > 0) {
      params.modifiedDatetimeStart = params.modifiedDatetime[0]
      params.modifiedDatetimeEnd = params.modifiedDatetime[1]
      delete params.modifiedDatetime
    }
    // 处理数据排序
    if (val.order) {
      val.order === 'asc' ? params.modifyTimeSort = 1 : params.modifyTimeSort = 0
    }
    return getSystemAccountListApi(params).then(res => {
      total.value = res.total
      console.log('查询数据', res)
      return {
        rows: res.data ? res.data : [] || []
      }
    })
  }

  // 搜索
  const search = async() => {
    if (schemaTableRef.value.localLoading) return
    schemaTableRef.value.refresh(true)
  }

  // 分页
  const handleSizeChange = async e => {
    searchForm.value.pageSize = e
    searchForm.value.pageIndex = 1
    search()
  }
  onMounted(() => {
    getBusinessLineList()
  })

  return {
    searchForm,
    formItems,
    columns,
    businessLineList,
    total,
    schemaTableRef,
    loadData,
    search,
    getBusinessLineList,
    handleSizeChange
  }
}
